package smartbooks.ui.users;

import java.awt.Container;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import smartbooks.exceptions.MyException;
import smartbooks.ui.listeners.CommonMouseListener;
import smartbooks.restrictDocuments.JTextFieldMaxLimitDocument;
import smartbooks.ui.SmartBooksUserSettings;
import smartbooks.ui.orgnanization.OrganizationFrame;
import smartbooks.ui.manager.UserManager;
import smartbooks.ui.util.CommonConstants;
import static smartbooks.ui.util.CommonConstants.CREATE_BUTTON_TEXT;
import static smartbooks.ui.util.CommonConstants.SAVE_BUTTON_TEXT;
import smartbooks.ui.util.CommonFunctions;
import smartbooksdb.exception.MandatoryFieldException;
import smartbooksdb.hibernatebean.User;

/**
 *
 * @author Ankit
 */
public class UsersPanel extends javax.swing.JPanel implements CommonConstants {
    private User editUser = null;
    User user = new User();
    String cpassword = new String();
    boolean defaultUserImageIcon = true;
    
    /**
     * Creates new form UsersPanel
     * @param editUser
     */
    public UsersPanel(User editUser) {
        if(null!=editUser) {
            this.editUser = editUser;
            this.user = editUser;
        }
        
        initComponents();
        
        customInitComponents();
    }
    
    /**
     * This method is used to get User
     * @return user
     */
    public User getUser() {
        return this.user;
    }
    
    /**
     * This method is used to set User
     * @param user
     */
    public void setUser(User user) {
        this.user = user;
    }
    
    /**
     * This method is used to get Confirm Password
     * @return cpassword
     */
    public String getCpassword() {
        return this.cpassword;
    }
    
    /**
     * This method is used to set Confirm Password
     * @param cpassword
     */
    public void setCpassword(String cpassword) {
        this.cpassword = cpassword;
    }
    
    private void customInitComponents() {
        addKeyListenersToComponents(keyListener);
        
        if(user.getUserId()==0) {
            userPanel.setBorder(new TitledBorder(userPanel.getBorder(), USER_PANEL_CREATE_TEXT, 2, 0, new Font("Default", 0, 14)));
            createOrSaveUserButton.setText(CREATE_BUTTON_TEXT);
        } else {
            userPanel.setBorder(new TitledBorder(userPanel.getBorder(), USER_PANEL_EDIT_TEXT, 2, 0, new Font("Default", 0, 14)));
            createOrSaveUserButton.setText(SAVE_BUTTON_TEXT);
        }
        
        editUserImageLabel.setVisible(false);
        deleteUserImageLabel.setVisible(false);
    }
    
    /**
     * This will enable/disable the Employee radio button.
     * e.g - When UsersFrame contains this UsersPanel then it will disable the Employee radio button.
     * 
     * @param enabled
     */
    public void setEnabledEmployeeRadioButton(boolean enabled) {
        employeeRadioButton.setEnabled(enabled);
    }
    
    private void assignAdditionalUserValues(String userModificationType) {
        try {
            user.setRole((roleButtonGroup.getSelection().equals(adminRadioButton.getModel())) ? USER_ROLE_ADMIN : USER_ROLE_EMPLOYEE);
            user.setGender((genderButtonGroup.getSelection().equals(maleRadioButton.getModel())) ? GENDER_MALE : GENDER_FEMALE);
            user.setDob(dOBDateChooser.getDate());
            if(!defaultUserImageIcon) {
                user.setPhoto(CommonFunctions.getBLOBImageFromIcon(userImageLabel.getIcon()));
            }
            
            if(userModificationType.equals(CREATE_BUTTON_TEXT)) {
                if(null!=SmartBooksUserSettings.USERNAME && !SmartBooksUserSettings.USERNAME.equals("")) {
                    user.setCreatedBy(SmartBooksUserSettings.USERNAME);
                } else {
                    user.setCreatedBy(usernameTextField.getText());
                }
            }
            
            user.setUpdatedBy(usernameTextField.getText());
        } catch (SQLException | IOException ex) {
            Logger.getLogger(UsersPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private void addKeyListenersToComponents(KeyListener keyListener) {
        usernameTextField.addKeyListener(keyListener);
        passwordPasswordField.addKeyListener(keyListener);
        cpasswordPasswordField.addKeyListener(keyListener);
        adminRadioButton.addKeyListener(keyListener);
        employeeRadioButton.addKeyListener(keyListener);
        firstNameLabel.addKeyListener(keyListener);
        middleNameLabel.addKeyListener(keyListener);
        lastNameLabel.addKeyListener(keyListener);
        stateComboBox.addKeyListener(keyListener);
        cityComboBox.addKeyListener(keyListener);
        maleRadioButton.addKeyListener(keyListener);
        femaleRadioButton.addKeyListener(keyListener);
        dOBDateChooser.getComponent(1).addKeyListener(keyListener);
        emailTextField.addKeyListener(keyListener);
        phoneTextField.addKeyListener(keyListener);
        mobileTextField.addKeyListener(keyListener);
        officeTextField.addKeyListener(keyListener);
        faxTextField.addKeyListener(keyListener);
        panNoFormattedTextField.addKeyListener(keyListener);
    }
    
    private void clearUserCredentialsPanel() {
        usernameTextField.setText("");
        passwordPasswordField.setText("");
        cpasswordPasswordField.setText("");
        adminRadioButton.setSelected(true);
        userImageLabel.setIcon(null);
    }
    
    private void clearUserDetailsPanel() {
        firstNameLabel.setText("");
        middleNameLabel.setText("");
        lastNameLabel.setText("");
        addressTextArea.setText("");
        stateComboBox.setSelectedIndex(0);
        cityComboBox.setSelectedIndex(0);
        maleRadioButton.setSelected(true);
        dOBDateChooser.setDate(null);
        emailTextField.setText("");
        phoneTextField.setText("");
        mobileTextField.setText("");
        officeTextField.setText("");
        faxTextField.setText("");
        panNoFormattedTextField.setText("");
        internalNotesTextArea.setText("");
    }
    
    private void setModifiedUserInParent() {
        Container c = this;
        
        while(c.getParent()!=null) {
            c = c.getParent();
            
            if(c instanceof UsersDialog) {
                ((UsersDialog)c).setModifiedUser(user);
            }
        }
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        genderButtonGroup = new javax.swing.ButtonGroup();
        roleButtonGroup = new javax.swing.ButtonGroup();
        userImageFileChooser = new javax.swing.JFileChooser();
        userPanel = new javax.swing.JPanel();
        userCredentialsPanel = new javax.swing.JPanel();
        jLabel19 = new javax.swing.JLabel();
        usernameTextField = new javax.swing.JTextField();
        jLabel20 = new javax.swing.JLabel();
        passwordPasswordField = new javax.swing.JPasswordField();
        jLabel21 = new javax.swing.JLabel();
        cpasswordPasswordField = new javax.swing.JPasswordField();
        roleLabel = new javax.swing.JLabel();
        adminRadioButton = new javax.swing.JRadioButton();
        employeeRadioButton = new javax.swing.JRadioButton();
        userImagePanel = new javax.swing.JPanel();
        editUserImageLabel = new javax.swing.JLabel();
        deleteUserImageLabel = new javax.swing.JLabel();
        userImageLabel = new javax.swing.JLabel();
        userDetailsPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        firstNameLabel = new javax.swing.JTextField();
        middleNameLabel = new javax.swing.JTextField();
        lastNameLabel = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        genderLabel = new javax.swing.JLabel();
        maleRadioButton = new javax.swing.JRadioButton();
        femaleRadioButton = new javax.swing.JRadioButton();
        dOBDateChooser = new com.toedter.calendar.JDateChooser("dd/MM/yyyy","##/##/####",' ');
        jLabel12 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel13 = new javax.swing.JLabel();
        emailTextField = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        phoneTextField = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        mobileTextField = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        officeTextField = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        faxTextField = new javax.swing.JTextField();
        jLabel18 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        internalNotesTextArea = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        addressTextArea = new javax.swing.JTextArea();
        stateComboBox = new javax.swing.JComboBox();
        cityComboBox = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        panNoFormattedTextField = new javax.swing.JFormattedTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        clearUserButton = new javax.swing.JButton();
        createOrSaveUserButton = new javax.swing.JButton();

        userImageFileChooser.setAcceptAllFileFilterUsed(false);
        userImageFileChooser.setFileFilter(new FileNameExtensionFilter("images", "jpg","jpeg","png"));

        userPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, " ", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14))); // NOI18N

        userCredentialsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Credentials", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N

        jLabel19.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel19.setText("<html><font color=red>* </font>Username:</html>");

        usernameTextField.setDocument(new JTextFieldMaxLimitDocument(16));
        usernameTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${user.username}"), usernameTextField, org.jdesktop.beansbinding.BeanProperty.create("text"), "usernameBinding");
        bindingGroup.addBinding(binding);

        usernameTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                usernameTextFieldKeyTyped(evt);
            }
        });

        jLabel20.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel20.setText("<html><font color=red>* </font>Password:</html>");

        passwordPasswordField.setDocument(new JTextFieldMaxLimitDocument(32));
        passwordPasswordField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${user.password}"), passwordPasswordField, org.jdesktop.beansbinding.BeanProperty.create("text"), "passwordBinding");
        bindingGroup.addBinding(binding);

        jLabel21.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel21.setText("<html><font color=red>* </font>Confirm Password:</html>");

        cpasswordPasswordField.setDocument(new JTextFieldMaxLimitDocument(32));
        cpasswordPasswordField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${cpassword}"), cpasswordPasswordField, org.jdesktop.beansbinding.BeanProperty.create("text"), "cpasswordBinding");
        bindingGroup.addBinding(binding);

        roleLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        roleLabel.setText("<html><font color=red>* </font>Role:</html>");

        roleButtonGroup.add(adminRadioButton);
        adminRadioButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        adminRadioButton.setSelected(true);
        adminRadioButton.setText("Admin");

        roleButtonGroup.add(employeeRadioButton);
        employeeRadioButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        employeeRadioButton.setText("Employee");

        userImagePanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)));
        userImagePanel.setPreferredSize(new java.awt.Dimension(100, 100));
        userImagePanel.setRequestFocusEnabled(false);
        userImagePanel.setLayout(null);

        editUserImageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/smartbooks/images/edit_16-16.png"))); // NOI18N
        editUserImageLabel.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        editUserImageLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                editUserImageLabelMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                editUserImageLabelMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                editUserImageLabelMouseExited(evt);
            }
        });
        userImagePanel.add(editUserImageLabel);
        editUserImageLabel.setBounds(0, 0, 16, 16);

        deleteUserImageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/smartbooks/images/delete_16-16.png"))); // NOI18N
        deleteUserImageLabel.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        deleteUserImageLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                deleteUserImageLabelMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                deleteUserImageLabelMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                deleteUserImageLabelMouseExited(evt);
            }
        });
        userImagePanel.add(deleteUserImageLabel);
        deleteUserImageLabel.setBounds(80, 0, 16, 16);

        userImageLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        userImageLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        userImageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/smartbooks/images/user-default.png"))); // NOI18N
        userImageLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                userImageLabelMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                userImageLabelMouseExited(evt);
            }
        });
        userImagePanel.add(userImageLabel);
        userImageLabel.setBounds(0, 0, 100, 100);

        javax.swing.GroupLayout userCredentialsPanelLayout = new javax.swing.GroupLayout(userCredentialsPanel);
        userCredentialsPanel.setLayout(userCredentialsPanelLayout);
        userCredentialsPanelLayout.setHorizontalGroup(
            userCredentialsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(userCredentialsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(userCredentialsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel19, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel20, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel21, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(roleLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(userCredentialsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(userCredentialsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(passwordPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(cpasswordPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(usernameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(userCredentialsPanelLayout.createSequentialGroup()
                        .addComponent(adminRadioButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(employeeRadioButton)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                .addComponent(userImagePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        userCredentialsPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cpasswordPasswordField, passwordPasswordField, usernameTextField});

        userCredentialsPanelLayout.setVerticalGroup(
            userCredentialsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(userCredentialsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(userCredentialsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(userImagePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(userCredentialsPanelLayout.createSequentialGroup()
                        .addGroup(userCredentialsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel19, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(usernameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(userCredentialsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel20, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(passwordPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(userCredentialsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel21, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cpasswordPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(userCredentialsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(roleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(adminRadioButton)
                            .addComponent(employeeRadioButton))))
                .addGap(0, 8, Short.MAX_VALUE))
        );

        userDetailsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Details", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel1.setText("Name:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel2.setText("Address:");

        firstNameLabel.setDocument(new JTextFieldMaxLimitDocument(32));
        firstNameLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${user.firstName}"), firstNameLabel, org.jdesktop.beansbinding.BeanProperty.create("text"), "firstnameBinding");
        bindingGroup.addBinding(binding);

        middleNameLabel.setDocument(new JTextFieldMaxLimitDocument(32));
        middleNameLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${user.middleName}"), middleNameLabel, org.jdesktop.beansbinding.BeanProperty.create("text"), "middlenameBinding");
        bindingGroup.addBinding(binding);

        lastNameLabel.setDocument(new JTextFieldMaxLimitDocument(32));
        lastNameLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${user.lastName}"), lastNameLabel, org.jdesktop.beansbinding.BeanProperty.create("text"), "lastnameBinding");
        bindingGroup.addBinding(binding);

        jLabel3.setText("Firstname");
        jLabel3.setEnabled(false);

        jLabel4.setText("Middlename");
        jLabel4.setEnabled(false);

        jLabel5.setText("Lastname");
        jLabel5.setEnabled(false);

        jLabel8.setText("City");
        jLabel8.setEnabled(false);

        jLabel9.setText("State");
        jLabel9.setEnabled(false);

        genderLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        genderLabel.setText("<html><font color=red>* </font>Gender:</html>");

        genderButtonGroup.add(maleRadioButton);
        maleRadioButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        maleRadioButton.setSelected(true);
        maleRadioButton.setText("Male");

        genderButtonGroup.add(femaleRadioButton);
        femaleRadioButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        femaleRadioButton.setText("Female");

        dOBDateChooser.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        dOBDateChooser.setMaxSelectableDate(new Date());
        dOBDateChooser.setMaximumSize(new java.awt.Dimension(91, 20));
        dOBDateChooser.setMinSelectableDate(new java.util.Date(-62135785720000L));
        dOBDateChooser.setMinimumSize(new java.awt.Dimension(91, 20));

        jLabel12.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel12.setText("DOB:");

        jLabel13.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel13.setText("Email:");

        emailTextField.setDocument(new JTextFieldMaxLimitDocument(64));
        emailTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${user.email}"), emailTextField, org.jdesktop.beansbinding.BeanProperty.create("text"), "emailBinding");
        bindingGroup.addBinding(binding);

        jLabel14.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel14.setText("Phone:");

        phoneTextField.setDocument(new JTextFieldMaxLimitDocument(16));
        phoneTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${user.phone}"), phoneTextField, org.jdesktop.beansbinding.BeanProperty.create("text"), "phoneBinding");
        bindingGroup.addBinding(binding);

        jLabel15.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel15.setText("Mobile:");

        mobileTextField.setDocument(new JTextFieldMaxLimitDocument(16));
        mobileTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${user.mobile}"), mobileTextField, org.jdesktop.beansbinding.BeanProperty.create("text"), "mobileBinding");
        bindingGroup.addBinding(binding);

        jLabel16.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel16.setText("Office:");

        officeTextField.setDocument(new JTextFieldMaxLimitDocument(16));
        officeTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${user.office}"), officeTextField, org.jdesktop.beansbinding.BeanProperty.create("text"), "officeBinding");
        bindingGroup.addBinding(binding);

        jLabel17.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel17.setText("Fax:");

        faxTextField.setDocument(new JTextFieldMaxLimitDocument(16));
        faxTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${user.fax}"), faxTextField, org.jdesktop.beansbinding.BeanProperty.create("text"), "faxBinding");
        bindingGroup.addBinding(binding);

        jLabel18.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel18.setText("Internal Notes:");

        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        internalNotesTextArea.setColumns(20);
        internalNotesTextArea.setDocument(new JTextFieldMaxLimitDocument(256));
        internalNotesTextArea.setLineWrap(true);
        internalNotesTextArea.setRows(3);
        internalNotesTextArea.setWrapStyleWord(true);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${user.notes}"), internalNotesTextArea, org.jdesktop.beansbinding.BeanProperty.create("text"), "internalNotesBinding");
        bindingGroup.addBinding(binding);

        internalNotesTextArea.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                internalNotesTextAreaKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(internalNotesTextArea);

        jScrollPane2.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        addressTextArea.setColumns(20);
        addressTextArea.setDocument(new JTextFieldMaxLimitDocument(256));
        addressTextArea.setLineWrap(true);
        addressTextArea.setRows(4);
        addressTextArea.setWrapStyleWord(true);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${user.address}"), addressTextArea, org.jdesktop.beansbinding.BeanProperty.create("text"), "addressBinding");
        bindingGroup.addBinding(binding);

        addressTextArea.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                addressTextAreaKeyPressed(evt);
            }
        });
        jScrollPane2.setViewportView(addressTextArea);

        stateComboBox.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        stateComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${user.state}"), stateComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"), "stateBinding");
        bindingGroup.addBinding(binding);

        cityComboBox.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        cityComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${user.city}"), cityComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"), "cityBinding");
        bindingGroup.addBinding(binding);

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel6.setText("PAN No:");

        try {
            panNoFormattedTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        panNoFormattedTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${user.panNumber}"), panNoFormattedTextField, org.jdesktop.beansbinding.BeanProperty.create("value"), "panBinding");
        bindingGroup.addBinding(binding);

        jLabel7.setText("<html><font color=red>*</font></html>");

        javax.swing.GroupLayout userDetailsPanelLayout = new javax.swing.GroupLayout(userDetailsPanel);
        userDetailsPanel.setLayout(userDetailsPanelLayout);
        userDetailsPanelLayout.setHorizontalGroup(
            userDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(userDetailsPanelLayout.createSequentialGroup()
                .addGroup(userDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, userDetailsPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(userDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(userDetailsPanelLayout.createSequentialGroup()
                                .addGroup(userDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(genderLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(userDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(userDetailsPanelLayout.createSequentialGroup()
                                        .addComponent(maleRadioButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(femaleRadioButton)
                                        .addGap(70, 70, 70)
                                        .addComponent(jLabel12)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(dOBDateChooser, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addGroup(userDetailsPanelLayout.createSequentialGroup()
                                        .addGroup(userDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(userDetailsPanelLayout.createSequentialGroup()
                                                .addComponent(jLabel9)
                                                .addGap(0, 0, Short.MAX_VALUE))
                                            .addComponent(stateComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(userDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel8)
                                            .addComponent(cityComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                            .addGroup(userDetailsPanelLayout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addGroup(userDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(userDetailsPanelLayout.createSequentialGroup()
                                        .addComponent(jLabel17)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(faxTextField))
                                    .addGroup(userDetailsPanelLayout.createSequentialGroup()
                                        .addComponent(jLabel15)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(mobileTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE))))
                            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(emailTextField, javax.swing.GroupLayout.Alignment.LEADING)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, userDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(userDetailsPanelLayout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 336, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, userDetailsPanelLayout.createSequentialGroup()
                            .addGap(67, 67, 67)
                            .addGroup(userDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(firstNameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(userDetailsPanelLayout.createSequentialGroup()
                                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 6, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGap(18, 18, 18)
                            .addGroup(userDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(middleNameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel4))
                            .addGap(18, 18, 18)
                            .addGroup(userDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel5)
                                .addComponent(lastNameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(jScrollPane1)
                    .addGroup(userDetailsPanelLayout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addGroup(userDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel13)
                            .addComponent(jLabel14)
                            .addComponent(jLabel16))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(userDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(userDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(phoneTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                                .addComponent(officeTextField))
                            .addComponent(panNoFormattedTextField)))
                    .addGroup(userDetailsPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel18)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );

        userDetailsPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {firstNameLabel, lastNameLabel, middleNameLabel});

        userDetailsPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {faxTextField, mobileTextField, officeTextField, phoneTextField});

        userDetailsPanelLayout.setVerticalGroup(
            userDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(userDetailsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(userDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel1)
                    .addComponent(firstNameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(middleNameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lastNameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(userDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(userDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jLabel7))
                .addGap(18, 18, 18)
                .addGroup(userDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(userDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(stateComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cityComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(userDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jLabel8))
                .addGap(18, 18, 18)
                .addGroup(userDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(genderLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(maleRadioButton)
                    .addComponent(femaleRadioButton)
                    .addComponent(jLabel12)
                    .addComponent(dOBDateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(userDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(emailTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(userDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel14)
                    .addComponent(phoneTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel15)
                    .addComponent(mobileTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(userDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel16)
                    .addComponent(officeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel17)
                    .addComponent(faxTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(userDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(panNoFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel18)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel10.setText("<html><font color=red>*</font></html>");

        jLabel11.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        jLabel11.setText("Required Field");

        clearUserButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        clearUserButton.setText("Clear");
        clearUserButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearUserButtonActionPerformed(evt);
            }
        });

        createOrSaveUserButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        createOrSaveUserButton.setText("Create");
        createOrSaveUserButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createOrSaveUserButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout userPanelLayout = new javax.swing.GroupLayout(userPanel);
        userPanel.setLayout(userPanelLayout);
        userPanelLayout.setHorizontalGroup(
            userPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(userPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(userPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(userPanelLayout.createSequentialGroup()
                        .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(168, 168, 168)
                        .addComponent(clearUserButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(createOrSaveUserButton))
                    .addGroup(userPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(userDetailsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(userCredentialsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        userPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {clearUserButton, createOrSaveUserButton});

        userPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {userCredentialsPanel, userDetailsPanel});

        userPanelLayout.setVerticalGroup(
            userPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(userPanelLayout.createSequentialGroup()
                .addGap(4, 4, 4)
                .addComponent(userCredentialsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(userDetailsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(userPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11)
                    .addComponent(clearUserButton)
                    .addComponent(createOrSaveUserButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(userPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(userPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        getAccessibleContext().setAccessibleName("");

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

    private void clearUserButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearUserButtonActionPerformed
        clearUserCredentialsPanel();
        clearUserDetailsPanel();
    }//GEN-LAST:event_clearUserButtonActionPerformed

    private void createOrSaveUserButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createOrSaveUserButtonActionPerformed
        CommonMouseListener.hideException = false;
        
        try {
            if(null==dOBDateChooser.getDate() && !((JTextField)dOBDateChooser.getComponent(1)).getText().equals(DATE_PATTERN)) {
                MyException me = new MyException("", "Invalid Date of birth");
                me.showBalloonTip((JComponent)dOBDateChooser.getComponent(1));
                ((JComponent)dOBDateChooser.getComponent(1)).requestFocus();
                return;
            }
            
            assignAdditionalUserValues(createOrSaveUserButton.getText());
            
            boolean userModified = false;
            switch (createOrSaveUserButton.getText()) {
                case CREATE_BUTTON_TEXT:
                    userModified = UserManager.createUser(user, cpasswordPasswordField.getPassword());
                    break;
                case SAVE_BUTTON_TEXT:
                    //userModified = UserManager.updateUser(user, cpasswordPasswordField.getPassword(), editUser);
                    break;
            }
            
            if(userModified) {
                Toolkit.getDefaultToolkit().beep();
                
                switch (createOrSaveUserButton.getText()) {
                    case CREATE_BUTTON_TEXT:
                        JOptionPane.showMessageDialog(this,"User is created successfully !","SmartBooks - Success",JOptionPane.INFORMATION_MESSAGE);
                        break;
                    case SAVE_BUTTON_TEXT:
                        JOptionPane.showMessageDialog(this,"User is updated successfully !","SmartBooks - Success",JOptionPane.INFORMATION_MESSAGE);
                        break;
                }
                
                setModifiedUserInParent();
                CommonFunctions.dispose(this);
                
                if(SmartBooksUserSettings.DEFAULT_ORG_ID==0) {
                    OrganizationFrame organizationFrame = new OrganizationFrame();
                }
            } else {
                Toolkit.getDefaultToolkit().beep();
                JOptionPane.showMessageDialog(this,"There was some error in creating User.\nKindly contact system administrator.","SmartBooks - Error",JOptionPane.ERROR_MESSAGE);
            }
        } catch (MyException me) {
            if(me.getComponentText().equalsIgnoreCase(USERNAME)) {
                me.showBalloonTip(usernameTextField);
                usernameTextField.requestFocus();
            } else if(me.getComponentText().equalsIgnoreCase(PASSWORD)) {
                me.showBalloonTip(passwordPasswordField);
                passwordPasswordField.setText("");
                if(me.getBalloonTipText().equals("Passwords mismatch")) {
                    cpasswordPasswordField.setText("");
                }
                passwordPasswordField.requestFocus();
            } else if(me.getComponentText().equalsIgnoreCase(ROLE)) {
                me.showBalloonTip(roleLabel);
            } else if(me.getComponentText().equalsIgnoreCase(FIRSTNAME)) {
                me.showBalloonTip(firstNameLabel);
                firstNameLabel.requestFocus();
            } else if(me.getComponentText().equalsIgnoreCase(GENDER)) {
                me.showBalloonTip(genderLabel);
            } else if(me.getComponentText().equalsIgnoreCase(STATE)) {
                me.showBalloonTip(stateComboBox);
                stateComboBox.requestFocus();
            } else if(me.getComponentText().equalsIgnoreCase(CITY)) {
                me.showBalloonTip(cityComboBox);
                cityComboBox.requestFocus();
            } else if(me.getComponentText().equalsIgnoreCase(EMAIL)) {
                me.showBalloonTip(emailTextField);
                emailTextField.requestFocus();
            } else if(me.getComponentText().equalsIgnoreCase(PAN)) {
                me.showBalloonTip(panNoFormattedTextField);
                panNoFormattedTextField.requestFocus();
            }
        } catch (MandatoryFieldException ex) {
            Logger.getLogger(UsersPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_createOrSaveUserButtonActionPerformed

    private void addressTextAreaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_addressTextAreaKeyPressed
        CommonFunctions.textboxTABPolicy(evt);
    }//GEN-LAST:event_addressTextAreaKeyPressed

    private void internalNotesTextAreaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_internalNotesTextAreaKeyPressed
        CommonFunctions.textboxTABPolicy(evt);
    }//GEN-LAST:event_internalNotesTextAreaKeyPressed

    private void userImageLabelMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_userImageLabelMouseEntered
        editUserImageLabel.setVisible(true);
        if(!defaultUserImageIcon) {
            deleteUserImageLabel.setVisible(true);
        }
    }//GEN-LAST:event_userImageLabelMouseEntered

    private void userImageLabelMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_userImageLabelMouseExited
        editUserImageLabel.setVisible(false);
        deleteUserImageLabel.setVisible(false);
    }//GEN-LAST:event_userImageLabelMouseExited

    private void editUserImageLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_editUserImageLabelMouseClicked
        try {
            int status = userImageFileChooser.showOpenDialog(this);
            if(status == JFileChooser.APPROVE_OPTION) {
                File f = userImageFileChooser.getSelectedFile();
                String filename = f.getAbsolutePath();
                ImageIcon ii = new ImageIcon(CommonFunctions.scaleImage((int)userImagePanel.getPreferredSize().getWidth(), (int)userImagePanel.getPreferredSize().getHeight(), ImageIO.read(new File(filename))));
                userImageLabel.setIcon(ii);
                defaultUserImageIcon = false;
            }
        } catch (Exception ex) {
            Logger.getLogger(UsersPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_editUserImageLabelMouseClicked

    private void deleteUserImageLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_deleteUserImageLabelMouseClicked
        userImageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/smartbooks/images/user-default.png")));
        deleteUserImageLabel.setVisible(false);
        defaultUserImageIcon = true;
    }//GEN-LAST:event_deleteUserImageLabelMouseClicked

    private void editUserImageLabelMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_editUserImageLabelMouseEntered
        editUserImageLabel.setVisible(true);
        if(!defaultUserImageIcon) {
            deleteUserImageLabel.setVisible(true);
        }
    }//GEN-LAST:event_editUserImageLabelMouseEntered

    private void editUserImageLabelMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_editUserImageLabelMouseExited
        editUserImageLabel.setVisible(false);
        deleteUserImageLabel.setVisible(false);
    }//GEN-LAST:event_editUserImageLabelMouseExited

    private void deleteUserImageLabelMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_deleteUserImageLabelMouseEntered
        editUserImageLabel.setVisible(true);
        if(!defaultUserImageIcon) {
            deleteUserImageLabel.setVisible(true);
        }
    }//GEN-LAST:event_deleteUserImageLabelMouseEntered

    private void deleteUserImageLabelMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_deleteUserImageLabelMouseExited
        editUserImageLabel.setVisible(false);
        deleteUserImageLabel.setVisible(false);
    }//GEN-LAST:event_deleteUserImageLabelMouseExited

    private void usernameTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_usernameTextFieldKeyTyped
        if(evt.getKeyChar() == ' ') {
            Toolkit.getDefaultToolkit().beep();
            evt.consume();
        }
    }//GEN-LAST:event_usernameTextFieldKeyTyped

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea addressTextArea;
    private javax.swing.JRadioButton adminRadioButton;
    private javax.swing.JComboBox cityComboBox;
    private javax.swing.JButton clearUserButton;
    private javax.swing.JPasswordField cpasswordPasswordField;
    private javax.swing.JButton createOrSaveUserButton;
    private com.toedter.calendar.JDateChooser dOBDateChooser;
    private javax.swing.JLabel deleteUserImageLabel;
    private javax.swing.JLabel editUserImageLabel;
    private javax.swing.JTextField emailTextField;
    private javax.swing.JRadioButton employeeRadioButton;
    private javax.swing.JTextField faxTextField;
    private javax.swing.JRadioButton femaleRadioButton;
    private javax.swing.JTextField firstNameLabel;
    private javax.swing.ButtonGroup genderButtonGroup;
    private javax.swing.JLabel genderLabel;
    private javax.swing.JTextArea internalNotesTextArea;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField lastNameLabel;
    private javax.swing.JRadioButton maleRadioButton;
    private javax.swing.JTextField middleNameLabel;
    private javax.swing.JTextField mobileTextField;
    private javax.swing.JTextField officeTextField;
    private javax.swing.JFormattedTextField panNoFormattedTextField;
    private javax.swing.JPasswordField passwordPasswordField;
    private javax.swing.JTextField phoneTextField;
    private javax.swing.ButtonGroup roleButtonGroup;
    private javax.swing.JLabel roleLabel;
    private javax.swing.JComboBox stateComboBox;
    private javax.swing.JPanel userCredentialsPanel;
    private javax.swing.JPanel userDetailsPanel;
    private javax.swing.JFileChooser userImageFileChooser;
    private javax.swing.JLabel userImageLabel;
    private javax.swing.JPanel userImagePanel;
    private javax.swing.JPanel userPanel;
    private javax.swing.JTextField usernameTextField;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    KeyListener keyListener = new KeyListener() {

        @Override
        public void keyTyped(KeyEvent e) {}

        @Override
        public void keyPressed(KeyEvent e) {}

        @Override
        public void keyReleased(KeyEvent e) {
            if(e.getKeyCode() == KeyEvent.VK_ENTER) { 
                createOrSaveUserButtonActionPerformed(null);
            }
        }
    };
}